package com.echo;

import android.app.Application;
import android.content.Intent;
import android.content.IntentFilter;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.query.Select;
import com.echo.model.DayData;
import com.echo.model.User;
import com.echo.receiver.DateChangeReceiver;
import com.echo.service.StepService;
import com.echo.util.FontsUtils;
import com.echo.util.SharedPreferencesUtils;

import cn.smssdk.SMSSDK;

/**
 * Application类，提供上下文对象
 * Created by EchoGe on 2016/4/23.
 */
public class SportsApplication extends Application{
    private static SportsApplication mSportsApplication;
    private static User mUser;
    private static boolean mLogin=false;
    private static DayData mToday;
    private static long mUserId;

    public static SportsApplication getInstance(){return mSportsApplication;}

    public SportsApplication(){
        super();
        mSportsApplication=this;
        //SMSSDK.initSDK(this,"13544c5336752","a41cfe14b136a2db32656543e7e4ccb1");

    }


    @Override
    public void onCreate(){
        super.onCreate();
        ActiveAndroid.initialize(this);
        FontsUtils.setDefaultFont(this,"MONOSPACE","fonts/Normal.otf");
        initStepService();
        initDateChangeReceiver();
        //autoLogin();
    }

    private void autoLogin() {
        /*long user_id = 0;
        user_id= (long) SharedPreferencesUtils.get(this,"auto_login_id",user_id);
        if(user_id!=0&&user_id!=-1) {
            mUser = User.load(User.class, user_id);
            if(mUser!=null) {
                SharedPreferencesUtils.put(this,"auto_login_id",0);
                mUserId= mUser.getid();
                mLogin = true;
            }
        }*/
    }

    public static void login(User user){
        mUser=user;
        mLogin=true;
    }

    public static void logout(){
        mUser=null;
        mLogin=false;
    }

    @Override
    public void onTerminate(){
        super.onTerminate();
        ActiveAndroid.dispose();
    }


    private void initDateChangeReceiver() {
    }

    private void initStepService() {
        Intent it=new Intent();
        it.setClass(this, StepService.class);
        startService(it);
    }

    public long getUserId(){
        return mUser.getId();
    }

    public String getUserNickname(){
        if(mUser.getNickname()==null||mUser.getNickname().equals(""))
            return mUser.getUsername();
        else
            return mUser.getNickname();
    }

    public String getUsername(){
        return mUser.getUsername();
    }

    public byte[] getUserHeadImage(){
        return mUser.getHeadImage();
    }

    public String getUserHeadImageUrl(){
        return mUser.getHeadImageUrl();
    }

    public String getUserPhone(){
        return mUser.getPhone();
    }

    public boolean isLogin(){
        if(mLogin==true&&mUser!=null&&mUser.getId()!=0)
            mLogin=true;
        else
            mLogin=false;
        return mLogin;
    }

    public User getUser(){
        return mUser;
    }

    public void setUser(User user){
        mUser=user;
    }
}
